<?php
defined('_XR_EXEC') or die();
/**
 * 逻辑处理类 
 */
class Pentype_Model extends Model
{ 
	/* 获取父亲节点路劲一直追溯到根节点 */
	public function loadparentmap($id){
		$db = factory::getdbo();
		$rs = $db->loadassoc('select * from t_pentype where id="'.$id.'" '); 
		if(!$rs){
			return array();
		}
		$ret=array();
		$ret[] = array('id'=>$rs['id'],'name'=>$rs['name']);
		if($rs['parent_id']<>'0'){  
			$this->find_parentsidname($rs['parent_id'],&$ret); 
		}
		$ret = array_reverse($ret);
		return $ret; 
	}
	function find_parentsidname( $id,&$cun ){
		$db = factory::getdbo();
		$rs = $db->loadassoc('select id,name,parent_id from `t_pentype` where  id="'.$id.'" '); 
		$cun[] = array('id'=>$rs['id'],'name'=>$rs['name']);
		if($rs['parent_id'] <> '0'){
			$this->find_parentsidname($rs['parent_id'],&$cun);
		}  
	}
	
	
	public function loadtypemapcount($id){
		$db = factory::getdbo();
		$rs = $db->loadassoc('select id,parent_id from t_pentype where id="'.$id.'" ');
		if(!$rs){
			return 0;
		}
		if($rs['parent_id']=='0'){
			$hid = $id;
		}else{
			# 找最高父节点
			$hid = $this->getHighParentID($id);
		} 
		$rs =  $db->loadassoc('select id from t_pentype where id="'.$hid.'" '); 
		# 如果没有子节点，就直接返回这个数组
		if($db->loadresult('select count(*) from t_pentype where parent_id="'.$rs['id'].'" ')==0){ 
			return 1;
		}
		$mapcount = 2;
		$two_rs = $db->loadassoclist('select id from t_pentype where parent_id="'.$rs['id'].'" ');
	  foreach($two_rs as &$v){
		  if($db->loadresult('select count(*) from t_pentype where parent_id="'.$v['id'].'" ')>0){
				$mapcount = 3;
				break;
			}
		} 
	  return $mapcount;
	}
	
	
  public function loadparentschilds($id){
		$db = factory::getdbo();
		$rs = $db->loadassoc('select id,parent_id from t_pentype where id="'.$id.'" ');
		if(!$rs){
			return array();
		}
		if($rs['parent_id']=='0'){
			$hid = $id;
		}else{
			# 找最高父节点
			$hid = $this->getHighParentID($id);
		}
		$ret =  $db->loadassoc('select * from t_pentype where id="'.$hid.'" '); 
		# 如果没有子节点，就直接返回这个数组
		if($db->loadresult('select count(*) from t_pentype where parent_id="'.$ret['id'].'" ')==0){
			$ret['childs'] = array();
			return array(
				0=>$ret
			);
		}
		$rs = $db->loadassoclist('select * from t_pentype where parent_id="'.$ret['id'].'" ');
		$ret = array();
	  foreach($rs as &$v){
			$v['childs'] = $db->loadassoclist('select * from t_pentype where parent_id="'.$v['id'].'" ');
			$ret[] = $v;
		} 
		return $ret;
	}
	
	public function getHighParentID($id){
		$db = factory::getdbo();
		$rs = $db->loadassoc('select id,parent_id from t_pentype where id="'.$id.'" ');
		if($rs['parent_id'] == '0'){
			return $id;
		}else{
			return $this->getHighParentID($rs['parent_id']);
		}
	}
	
 	
	/* 获取全部父节点 */
	public function loadparents($id){
		$db = factory::getdbo();
		$rs = $db->loadassoc('select * from t_pentype where id="'.$id.'" ');
		if(!$rs){
			return array();
		}
		$parent_id=0;
		if($rs['isleaf']=='0'){
			$parent_id = $rs['id'];
		}else{
			$parent_id = $rs['parent_id'];
		}
		if($parent_id=='0'){
			return $db->loadassoclist('select * from t_pentype where id="'.$id.'" ');
		}else{
			return $db->loadassoclist('select * from t_pentype where parent_id="'.$parent_id.'" order by show_index asc ');
		}	
	}
	 
	
	/* 获取全部子叶子节点 */
	public function leafchilds($id){
 
		$db = factory::getdbo();
		$rs = $db->loadassoc('select * from t_pentype where id="'.$id.'" ');
		if(!$rs){
			return array();
		}
		$ret=array();
		 
		if($rs['isleaf']=='1'){
			$ret[] = $rs['id'];
		}else{
			$childtsmap = array();
			$this->find_childtmap($rs['id'],&$childtsmap);
			$ret = $childtsmap;
		}
		 return $ret;
	}
	function find_childtmap( $id,&$cun ){
		$db = factory::getdbo(); 
		if($id == '0'){
			return '';
		}
		$rstemp = $db->loadassoclist('select id,isleaf from `t_pentype` where  parent_id="'.$id.'" order by show_index asc ');
		foreach($rstemp as $v){
			if($v['isleaf'] == '1'){
				$cun[] = $v['id'];
			}else{
				$this->find_childtmap($v['id'],&$cun);
			} 
		}
	}
	
	
	
	
	/* 循环类别 */
	function load_type( ){
		$rs = array();
		$db = factory::getdbo();
		$rs = $db->loadassoclist('select * from `t_pentype` where   parent_id=0 order by show_index,id asc ');
		$this->type_map($rs );
		return $rs;
	}
	function type_map(&$rs ){
		$db = factory::getdbo();
		foreach($rs as &$v){
			$temp = $db->loadassoclist('select * from `t_pentype` where  parent_id="'.$v['id'].'" order by show_index,id asc ');
			if(count($temp)>0) $this->type_map($temp,$mid);
			$v['childs'] = $temp;
		}
	}
	
	

	
	
	/* 找叶子节点 */
	function load_isleaf($index = ''){
		$rs = array();
		$db = factory::getdbo();
		if($index == ''){
			$rs = $db->loadassoclist('select * from `t_pentype` where   isleaf=1 order by parent_id,show_index asc,id asc ');
		}else{
			$rs = $db->loadassoclist('select * from `t_pentype` where   isleaf=1 order by parent_id,show_index asc,id asc ',$index);
		}
		
  
		foreach($rs as &$v){
			if($v['parent_id'] != '0'){
				$tempmap = $this->find_parentmap($v['parent_id']);
				$tempmap = explode('-',$tempmap);
				$tempmap = array_reverse($tempmap);
				$v['parentmap'] = implode('-',$tempmap).' - '.$v['name']; 
			}else{
				$v['parentmap'] = $v['name']; 
			}
		}  
		return $rs;
	}
	function find_parentmap( $parent_id ){
		$db = factory::getdbo(); 
		if($parent_id == '0'){
			return '';
		}
		$rstemp = $db->loadassoc('select parent_id,name from `t_pentype` where  id="'.$parent_id.'" ');
		if($rstemp['parent_id'] == '0'){
			return $rstemp['name'];
		}else{
			return $rstemp['name'].'-'.$this->find_parentmap($rstemp['parent_id']);
		}
	}
	
	# 获取信息全路径
	public function findfullpath($id){
		$db = factory::getdbo(); 
		$v = $db->loadassoc('select  parent_id,name from t_pentype where id="'.$id.'" '); 
		if($v['parent_id'] != '0'){
			$tempmap = $this->find_parentmap($v['parent_id']);
			$tempmap = explode('-',$tempmap);
			$tempmap = array_reverse($tempmap);
			$v['parentmap'] = implode('-',$tempmap).' - '.$v['name']; 
		}else{
			$v['parentmap'] = $v['name']; 
		} 
		return $v['parentmap']; 
	}
  
}

?>